package com.example.bankingsystemandroid.task;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.bankingsystemandroid.LoginActivity;
import com.example.bankingsystemandroid.MainActivity;
import com.example.bankingsystemandroid.utils.PropertiesConfig;
import com.example.bankingsystemandroid.utils.PropertyManager;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

/**
 * 
 * @author angelkid
 *
 */
public class TransferMoneyTask extends AsyncTask<ArrayList<NameValuePair>, Void, String>{
	private Context context;

	public TransferMoneyTask(Context context) {
		super();
		this.context = context.getApplicationContext();
	}
	
	@Override
	protected String doInBackground(ArrayList<NameValuePair>... params) {
		/*
		 * Create the POST request
		 */
		HttpClient httpClient = new DefaultHttpClient();
		String serverIP = PropertiesConfig.getProperty(PropertyManager.SERVER_IP);
		String serverPort = PropertiesConfig.getProperty(PropertyManager.SERVER_PORT);
		
		HttpPost httpPost = new HttpPost("http://"+serverIP+":"+serverPort+"/bankingsystem/api/transfer-money");
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(params[0]));
		} catch (UnsupportedEncodingException e2) {
			e2.printStackTrace();
		}		
		/*
		 * Execute the HTTP Request
		 */
		try {			
			httpPost.addHeader("Cookie", "JSESSIONID=" + InfoHolder.getSessionId());
			
			HttpResponse response = httpClient.execute(httpPost);
		    HttpEntity respEntity = response.getEntity();
		    if(respEntity != null) {
				try {
					String retSrc = EntityUtils.toString(respEntity);
					JSONObject result = new JSONObject(retSrc);
					String resultCode = result.getString("code");
					return resultCode;
				} catch (ParseException e) {
					e.printStackTrace();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				}
				catch (JSONException e) {
					e.printStackTrace();
				}
				// {"token":"414b64a3-0b3e-4f0e-b1df-868d70b71bf9","loginId":"test04","errorCode":null,"errorMsg":null}
				// {"token":null,"loginId":null,"errorCode":"CAPTCHA_NOT_MATCH","errorMsg":"Captcha not match."}
			}
		    
		} catch (ClientProtocolException e) {
		    // writing exception to log
		    e.printStackTrace();
		} catch (IOException e) {
		    // writing exception to log
		    e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		
		if ("SUCCESS".equals(result)) {
//			context.startActivity(new Intent(context, LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
			Toast.makeText(context, "Transfer successful", Toast.LENGTH_SHORT).show();
		} else if ("TRANSFER_INVALID_SOURCE".equals(result)) {
			Toast.makeText(context, "Invalid source account number", Toast.LENGTH_SHORT).show();
		} else if ("TRANSFER_INVALID_DESTINATION".equals(result)) {
			Toast.makeText(context, "Invalid destination account number!", Toast.LENGTH_SHORT).show();
		} else if ("TRANSFER_NOT_ENOUGH_MONEY".equals(result)){
			Toast.makeText(context, "The money to transfer is greater than balance!", Toast.LENGTH_SHORT).show();
		}
		super.onPostExecute(result);
	}
	
	

}
